<?php
include(__APP_PATH . '/model/comment.class.php');
include(__APP_PATH . '/model/image.class.php');
include(__APP_PATH . '/model/gallery.class.php');

/**
 * This controllers handles all /image pages
 * 
 * @author johan <info@johandahlberg.com>
 */
class ImageController extends BaseController {
	/**
	 * (non-PHPdoc)
	 * @see BaseController::index()
	 */
	public function index() {
        $this->registry->template->image_heading = 'This is the image Index';
        $this->registry->template->show('image_index');
	}
	
	/**
	 * Handles the /image/view page
	 */
	public function view() {
		$form_message = '<ol class="formMessages">';
		$oGallery = new Gallery(__APP_PATH . '/data/gallery.xml');
		$params = array(
			'name' 		=> "",
			'email' 	=> "",
			'text' 		=> "",
			'rating' 	=> "",
			'ipAddress' => $_SERVER["REMOTE_ADDR"],
			'datetime' 	=> date(DATE_ATOM)
		);
		$valid = true;
		
		// Does image exist?
		if($this->registry->router->args[0] >= 0) {
			$this->registry->template->image_heading = 'Bild: ' . $this->registry->router->args[0];			
			//Is there a comment posted
			if(isset($_POST['submit'])) {
				// If name is isset
				if(isset($_POST['commentName'])) {
					$params['name'] = $_POST['commentName'];
				} else {
					$valid = false;
					$params['name'] = $_POST['commentName'];
					$form_message .= "<li>Name is not correcly filled</li>";
				}
				
				// If email is isset
				if($_POST['commentEmail'] != null) {
					// Is e-mail in the correct format?
					if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['commentEmail'])) {
						$valid = false;
						$form_message .= '<li>E-mail is not in correct format</li>';
						$params['email'] = $_POST['commentEmail'];	
					} else {
						$params['email'] = $_POST['commentEmail'];
					}
				} else {
					$valid = false;
					$params['email'] = $_POST['commentEmail'];
					$form_message .= '<li>E-mail is not correcly filled</li>';
				}
				
				// If rating is isset
				if($_POST['commentRating'] != "-") {
					$params['rating'] = $_POST['commentRating'];
				} else {
					$valid = true;
					$params['rating'] = null;
					//$form_message .= '<li>Rating is not correcly filled</li>';
				}
				
				// If name is isset
				if($_POST['commentName'] != null) {
					$params['text'] = htmlspecialchars($_POST['commentText']);
				} else {
					$valid = false;
					$params['text'] = htmlspecialchars($_POST['commentText']);
					$form_message .= '<li>Text is not correcly filled</li>';
				}
				
				// if everything is valid
				if($valid) {
					$oGallery->addComment($this->registry->router->args[0], $params['name'], $params['email'], $params['datetime'], $params['rating'], $params['text'], $params['ipAddress']);
					
					$params = array(
						'name' 		=> "",
						'email' 	=> "",
						'text' 		=> "",
						'rating' 	=> "",
						'ipAddress' => $_SERVER["REMOTE_ADDR"],
						'datetime' 	=> date(DATE_ATOM)
					);
					$form_message .= '<li>Thank you, for your comment</li>';
				}
			}
		} else {
			$this->registry->template->image_heading = 'Bilden kan inte hittas';
			$this->registry->template->image_content = 'Tyvärr kunde vi inte hitta bilden du letade efter...';
		}
		
		$form_message .= '</ol>';
		
		$this->registry->template->form_message = $form_message;
		$this->registry->template->oGallery = $oGallery;
		$this->registry->template->params = $params;
        $this->registry->template->show('image_view');
	}
}
?>